<?php

namespace App\Kuafu\Product\Validate;

use App\Http\Validate\AbstractBaseValidate;
use App\Kuafu\Product\Enum\CodeEnum;
use App\Kuafu\Product\Enum\ProductCodeEnum;
use App\Kuafu\Product\Model\Product;
use Illuminate\Http\Request;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Base\Validate\BaseValidate;

class ProductValidate extends BaseValidate
{

    public static function addProduct(Request $request): string
    {
        $rules = [
            'name'        => [
                'required',
                function ($attribute, $value, $fail) use ($request) {
                    if (Product::query()
                               ->where('name', '=', $request['name'])
                               ->where('menuId', '=', 0)
                               ->first()) {
                        $fail("菜品名重复，请修改");
                    }
                }
            ],
            'brandId'     => 'required|integer',
            'categoryId'  => 'required|integer',
            'kind'        => AbstractBaseValidate::intInValidateBuild(true, CodeEnum::KIND_MAP),
            'desc'        => 'required',
            'imageUrl'    => 'required',
            'price'       => 'required|int',
//            'vipPrice'      => 'required',
//            'costPrice'     => 'required',
            "isHot"       => "required|bool",
            "isRecommend" => "required|bool",
            //todo 套餐
//            'showPlatform' => 'required|integer',
//            'type'         => 'required|integer'
        ];
        if ($request->method() == 'PUT') {
            $rules['name'] = [
                'required',
                'string',
                'max:20',
                function ($attribute, $value, $fail) use ($request) {
                    if (Product::query()
                               ->where('name', '=', $request['name'])
                               ->where('id', '!=', $request->productId)
                               ->where("isDeleted", '=', 0)
                               ->where('menuId', '=', 0)
                               ->first()) {
                        $fail("菜品名重复，请修改");
                    }
                }
            ];
        }
        $messages = [
            'brandId.required'    => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_BRAND_NOT_INPUT],
            'name.required'       => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_NAME_NOT_INPUT],
            'categoryId.required' => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_CATE_NOT_SELECT],
            'categoryId.integer'  => BaseCodeEnum::CODE_TEXT[BaseCodeEnum::ERR_400009],
            'desc.required'       => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_DESC_NOT_INPUT],
            'imageUrl.required'   => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_IMAGE_NOU_INPUT],
            'price.required'      => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_PRICE_NOT_INPUT],
            'vipPrice.required'   => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_VIP_PRICE_NOT_INPUT],
            'costPrice.required'  => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_COST_PRICE_NOT_INPUT],
            'kind.required'       => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_KIND_NOT_INPUT],
        ];

        return self::validateCheck($request->all() + ['brandId' => $request->header('brandId')], $rules, $messages);
    }


    public static function setProductValidate(Request $request): string
    {
        $rules = [
            'name'        => [
                'required',
                function ($attribute, $value, $fail) use ($request) {
                    if (Product::query()
                               ->where('name', '=', $request['name'])
                               ->where('menuId', '=', 0)
                               ->first()) {
                        $fail("菜品名重复，请修改");
                    }
                }
            ],
            'brandId'     => 'required|integer',
            'categoryId'  => 'required|integer',
            'desc'        => 'required',
            'imageUrl'    => 'required',
            'price'       => 'required_if:kind,'.CodeEnum::KIND_NORMAL.'|int',
//            'vipPrice'      => 'required',
//            'costPrice'     => 'required',
            "isHot"       => "required|bool",
            "isRecommend" => "required|bool",
            //todo 套餐
//            'showPlatform' => 'required|integer',
//            'type'         => 'required|integer'
        ];
        if ($request->method() == 'PUT') {
            $rules['name'] = [
                'required',
                'string',
                'max:20',
                function ($attribute, $value, $fail) use ($request) {
                    if (Product::query()
                               ->where('name', '=', $request['name'])
                               ->where('id', '!=', $request->productId)
                               ->where("isDeleted", '=', 0)
                               ->where('menuId', '=', 0)
                               ->first()) {
                        $fail("菜品名重复，请修改");
                    }
                }
            ];
        }
        $messages = [
            'brandId.required'    => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_BRAND_NOT_INPUT],
            'name.required'       => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_NAME_NOT_INPUT],
            'categoryId.required' => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_CATE_NOT_SELECT],
            'categoryId.integer'  => BaseCodeEnum::CODE_TEXT[BaseCodeEnum::ERR_400009],
            'desc.required'       => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_DESC_NOT_INPUT],
            'imageUrl.required'   => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_IMAGE_NOU_INPUT],
            'price.required'      => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_PRICE_NOT_INPUT],
            'vipPrice.required'   => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_VIP_PRICE_NOT_INPUT],
            'costPrice.required'  => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_COST_PRICE_NOT_INPUT],
            'kind.required'       => ProductCodeEnum::CODE_TEXT[ProductCodeEnum::ERR_PRODUCT_KIND_NOT_INPUT],
        ];

        return self::validateCheck($request->all() + ['brandId' => $request->header('brandId')], $rules, $messages);
    }


    public static function bindMenu($data)
    {
        $rules = [
            'productMenuId' => 'required|integer|min:0',
            'productIds'    => [
                'nullable',
                'array',
                function ($attribute, $value, $fail) use ($data) {
                    foreach ($data['productIds'] ?? [] as $v) {
                        if (!is_int($v)) {
                            $fail('productIds必须为int数组');
                        }
                    }
                }
            ],
        ];

        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }


    public static function doStatus(Request $request)
    {
        $rules = [
            'status' => 'nullable|integer|between:0,2',
            'out'    => 'nullable|integer|between:0,2',
        ];

        $messages = [];

        return self::validateCheck($request->toArray(), $rules, $messages);
    }

    public static function checkPackageData(Request $request): string
    {
        $rules    = [
            'productList' => [
                'array',
                function ($attribute, $value, $fail) use ($request) {
                    if (empty($request->productList)) {
                        $fail('请选择套餐商品');
                    }
                    foreach ($request->productList as $v) {
                        if (!is_int($v['productId']) || !is_int($v['num'])) {
                            $fail('菜品id和菜品数量必须是int类型');
                        }
                    }
                }
            ]
        ];
        $messages = [];

        return self::validateCheck($request->all(), $rules, $messages);
    }
}
